package com.junjie.checkin.controller;

import com.junjie.checkin.request.gift.ExchangeAddressRequest;
import com.junjie.checkin.request.gift.GiftFrontPageRequest;
import com.junjie.checkin.request.gift.GiftPageRequest;
import com.junjie.checkin.response.PageResult;
import com.junjie.checkin.response.Result;
import com.junjie.checkin.response.ResultStatus;
import com.junjie.checkin.response.front.GiftDetailsFrontResponse;
import com.junjie.checkin.response.front.GiftFrontResponse;
import com.junjie.checkin.service.GiftService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 洪浚杰
 * @description 礼物控制器
 * @date 2025/6/16 09:07
 */
@RestController
@RequestMapping("/gifts")
public class GiftController {
    @Autowired
    private GiftService giftService;

    /**
     * 获取礼物列表
     * @param request 分页参数
     * @return 礼物列表
     */
    @GetMapping("")
    public Result<PageResult<GiftFrontResponse>> getGifts(GiftFrontPageRequest request) {
        request.initDefaults();
        PageResult<GiftFrontResponse> frontGiftList = giftService.getFrontGiftList(request);
        return Result.getResult(ResultStatus.OK,frontGiftList);
    }


    /**
     * 获取礼物详情
     * @param id 礼物id
     * @return 礼物详情数据
     */
    @GetMapping("/{id}")
    public Result<GiftDetailsFrontResponse> getGiftById(@PathVariable Integer id) {
        GiftDetailsFrontResponse giftDetails = giftService.getFrontGiftById(id);
        return Result.getResult(ResultStatus.OK,giftDetails);
    }


    /**
     * 兑换礼物
     * @param id 礼物id
     * @param address 收货地址
     * @param request HTTP请求, 用于获取用户信息
     * @return 统一结果,无返回值
     */
    @PostMapping("/{id}")
    public Result exchangeGift(@PathVariable Integer id, @Validated  @RequestBody ExchangeAddressRequest address, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        giftService.exchangeGift(id,address,token);
        return Result.getResult(ResultStatus.OK);
    }

}
